﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using AutoMapper;
using DTO;
namespace DAO
{
    public class ChuyenBay_DAO
    {
        public static ChuyenBay_DTO CreateChuyenBayDTO(ChuyenBay chuyenbay)
        {
            ChuyenBay_DTO chuyenbayDTO = new ChuyenBay_DTO();
            Mapper.CreateMap<ChuyenBay, ChuyenBay_DTO>();
            chuyenbayDTO = Mapper.Map<ChuyenBay, ChuyenBay_DTO>(chuyenbay);
            return chuyenbayDTO;
        }
        
        public BindingList<ChuyenBay_DTO> GetChuyenBayList()
        {
            BindingList<ChuyenBay_DTO> listChuyenBay = new BindingList<ChuyenBay_DTO>();
           try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from chuyenbay in context.ChuyenBays select chuyenbay;
                foreach (var cb in query)
                {
                    ChuyenBay_DTO chuyenbayDTO = CreateChuyenBayDTO(cb);
                    listChuyenBay.Add(chuyenbayDTO);
                }
            }
           catch (System.Exception ex)
           {
               string str = ex.Message;
           }
            return listChuyenBay;
        }

        public ChuyenBay_DTO GetChuyenBayDetails(string maChuyenBay)
        {
            ChuyenBay_DTO chuyenbayDTO = new ChuyenBay_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from cb in context.ChuyenBays
                            where cb.MaChuyenBay == maChuyenBay
                            select cb;
                ChuyenBay chuyenbayEF = query.First();
                chuyenbayDTO = CreateChuyenBayDTO(chuyenbayEF);
            }
            catch (System.Exception)
            {
            }
            return chuyenbayDTO;
        }

        public static string MaChuyenBayMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from cb in context.ChuyenBays select cb.MaChuyenBay).Max();
            return query;
        }

        public void InsertChuyenBay(ChuyenBay_DTO chuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                ChuyenBay chuyenbay = new ChuyenBay();
                chuyenbay.MaChuyenBay = chuyenbayDTO.MaChuyenBay;
                chuyenbay.MaTuyenBay = chuyenbayDTO.MaTuyenBay;
                chuyenbay.Ngay_Gio = chuyenbayDTO.Ngay_Gio;
                chuyenbay.ThoiGianBay = chuyenbayDTO.ThoiGianBay;
                chuyenbay.SoLuongGheHang1 = chuyenbayDTO.SoLuongGheHang1;
                chuyenbay.SoLuongGheHang2 = chuyenbayDTO.SoLuongGheHang2;
                context.ChuyenBays.InsertOnSubmit(chuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void UpdateChuyenBay(ChuyenBay_DTO chuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                ChuyenBay chuyenbay = new ChuyenBay();
                chuyenbay = context.ChuyenBays.Single(cb => cb.MaChuyenBay == chuyenbayDTO.MaChuyenBay);
                chuyenbay.MaChuyenBay = chuyenbayDTO.MaChuyenBay;
                chuyenbay.MaTuyenBay = chuyenbayDTO.MaTuyenBay;
                chuyenbay.Ngay_Gio = chuyenbayDTO.Ngay_Gio;
                chuyenbay.ThoiGianBay = chuyenbayDTO.ThoiGianBay;
                chuyenbay.SoLuongGheHang1 = chuyenbayDTO.SoLuongGheHang1;
                chuyenbay.SoLuongGheHang2 = chuyenbayDTO.SoLuongGheHang2;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteChuyenBay(ChuyenBay_DTO chuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                ChuyenBay chuyenbay = context.ChuyenBays.Single(cb => cb.MaChuyenBay == chuyenbayDTO.MaChuyenBay);
                context.ChuyenBays.DeleteOnSubmit(chuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }
    }
}
